﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetPE.Core.DataDirectories;

namespace NetPE
{
    public partial class ResourcesEditor : BaseEditor
    {

        private SplitContainer can;
        private TreeView tv;
        private PropertyEditor prop;

        public ResourcesEditor()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            can = new SplitContainer();
            tv = new TreeView();
            prop = new PropertyEditor();
            can.Panel1.SuspendLayout();
            can.Panel2.SuspendLayout();
            can.SuspendLayout();
            this.SuspendLayout();

            can.Dock = DockStyle.Fill;
            can.Orientation = Orientation.Horizontal;

            can.Panel1.Controls.Add(tv);

            can.Panel2.BackColor = Color.Black;
            can.Panel2.Controls.Add(prop);
            can.Panel2.ForeColor = Color.White;

            tv.BorderStyle = BorderStyle.None;
            tv.Dock = DockStyle.Fill;
            tv.AfterSelect += new TreeViewEventHandler(tvAfterSelect);

            prop.Dock = DockStyle.Fill;
            prop.Font = new Font("Arial", 9F);

            this.Controls.Add(can);
            can.Panel1.ResumeLayout(false);
            can.Panel2.ResumeLayout(false);
            can.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        void tvAfterSelect(object sender, TreeViewEventArgs e)
        {
            prop.Hide();
            prop.Object = e.Node.Tag;
            prop.Show();
        }

        protected override void RefreshData()
        {
            TreeNode root = new TreeNode("Resource Directory");
            root.Tag = Object;
            ResourceDirectory res = Object as ResourceDirectory;
            foreach (ResourceDirectory.ResourceEntry i in res)
            {
                ProcessEntry(root, i);
            }
            tv.Nodes.Clear();
            tv.Nodes.Add(root);
            prop.Hide();
        }

        private void ProcessEntry(TreeNode par, ResourceDirectory.ResourceEntry rEntry)
        {
            TreeNode entry = new TreeNode();
            entry.Text = (rEntry.Type & ResourceDirectory.EntryType.DataEntry) == ResourceDirectory.EntryType.DataEntry ? "Data Entry" : "Subdirectory";
            entry.Text += (rEntry.Type & ResourceDirectory.EntryType.ID) == ResourceDirectory.EntryType.ID ? ", ID : " : ", Name : ";
            entry.Text += (rEntry.Type & ResourceDirectory.EntryType.ID) == ResourceDirectory.EntryType.ID ? rEntry.ID.ToString(): rEntry.Name;
            entry.Tag = rEntry.Data;
            par.Nodes.Add(entry);

            if ((rEntry.Type & ResourceDirectory.EntryType.DataEntry) == ResourceDirectory.EntryType.DataEntry)
            {
                return;
            }
            else
            {
                foreach (ResourceDirectory.ResourceEntry i in rEntry.Data as ResourceDirectory.Subdirectory)
                {
                    ProcessEntry(entry, i);
                }
            }
        }
    }
}
